﻿using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace be.metanous.mvvm.extensions
{
    /// <summary>
    /// Ruby-style foreach on IEnumerable<T> collections.
    /// e.g. enumerableObject.Each(val => Console.WriteLine(val)); 
    /// </summary>
    [DebuggerStepThrough]
    public static class EnumerableExtensions
    {

        public static void Each<TSource>(this IEnumerable<TSource> source, Action<TSource> action)
        {
            if (source == null || action == null) return;
            foreach (var item in source)
            {
                action(item);
            }
        }

        public static void EachIndex<TSource>(this IEnumerable<TSource> source, Action<int> action)
        {
            if (source == null || action == null) return;
            var i = 0;
            var enumerator = source.GetEnumerator();
            while (enumerator.MoveNext())
            {
                action(i++);
            }
        }

    }

}
